package com.sog.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sog.common.DBUtils;
import com.sog.entity.Logs;
public class LogsDaoImpl implements LogsDaoI<Logs> {

	
	public boolean insert(Logs t) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "INSERT INTO logs (operate_content,operate_time,security_class,if_success,operate_type) VALUES(?,?,?,?,?)";
		// 创建PreparedStatement
		PreparedStatement ps= conn.prepareStatement(sql);
		
			ps = conn.prepareStatement(sql);
			ps.setString(1, t.getContent());
			ps.setDate(2, new Date(new java.util.Date().getTime()));
			ps.setInt(3,t.getSecurity_class());
			ps.setBoolean(4, t.isIf_success());
			ps.setString(5,t.getOperate_type());
			if (ps.executeUpdate() > 0) {
				return true;
			}
			DBUtils.close(conn, ps, null);
		    return false;
	}

	public boolean update(Logs t) throws Exception {
		Connection conn = DBUtils.getConnection();
		String sql = "UPDATE logs SET operate_content=?,operate_time=?,security_class=?,if_success=?,operate_type=? where logs_id=?";
		// 创建PreparedStatement
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, t.getContent());
		ps.setDate(2, new Date(new java.util.Date().getTime()));
		ps.setInt(3,t.getSecurity_class());
		ps.setBoolean(4, t.isIf_success());
		ps.setString(5,t.getOperate_type());
		ps.setInt(6, t.getId());
		if (ps.executeUpdate() > 0) {
			return true;
		}
		DBUtils.close(conn, ps, null);
		
		return false;
	}

	public boolean delete(int id) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "DELETE FROM logs WHERE logs_id=?";
		// 创建PreparedStatement
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, id);
		if (ps.executeUpdate() > 0) {
			return true;
		}
		DBUtils.close(conn, ps, null);
		return false;
	
	}


	
	public List<Logs> selectWhere(String whereSql) throws SQLException {
		Connection conn = DBUtils.getConnection();
		StringBuffer sb=new StringBuffer();
		sb.append("SELECT logs_id,operate_content,operate_time,security_class,if_success,operate_type ");
		sb.append("FROM logs ");
		sb.append(" WHERE 1=1 ");
		sb.append(whereSql);
		System.out.println(sb.toString());
		
		PreparedStatement ps = conn.prepareStatement(sb.toString());	
		List<Logs>  list= new ArrayList<Logs>();
		ResultSet rs=ps.executeQuery();
		while (rs.next()) {
			Logs  t=new Logs();
			t.setId(rs.getInt(1));
			t.setContent(rs.getString(2));
			t.setOperateDate(rs.getDate(3));
			t.setSecurity_class(rs.getInt(4));
			t.setIf_success(rs.getBoolean(5));
			t.setOperate_type(rs.getString(6));
			
			list.add(t);
		}
		DBUtils.close(conn, ps, rs);
		return list;
		
	
	}

	public List<Logs> selectAll() throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "SELECT logs_id,operate_content,operate_time,security_class,if_success,operate_type FROM logs";
		// 创建PreparedStatement
		PreparedStatement ps = conn.prepareStatement(sql);
		//将数据库中的记录转换为对象
		List<Logs>  list=new ArrayList<Logs>();
		//结果集
		ResultSet rs=ps.executeQuery();
		while (rs.next()) {
			Logs t=new Logs();
			t.setId(rs.getInt(1));
			t.setContent(rs.getString(2));
			t.setOperateDate(rs.getDate(3));
			t.setSecurity_class(rs.getInt(4));
			t.setIf_success(rs.getBoolean(5));
			t.setOperate_type(rs.getString(6));
			list.add(t);
		}
		DBUtils.close(conn, ps, rs);
		return list;
	}

	@Override
	public Map<String, Object> selectPage(String whereSql, int currentPage, int pageSize) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
